package com.nwpu.bigdata.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.nwpu.bigdata.model.Rule;
import com.nwpu.bigdata.service.RuleService;

@RestController
@RequestMapping("/rule")
public class RuleController {
	private static Logger logger = LoggerFactory.getLogger(LogController.class);

    @Autowired
    RuleService ruleService;
  
    
    @PostMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public int insertRuleToRuleTable(@RequestParam(value = "ruleName", required = true) String ruleName,
			                          @RequestParam(value = "hostName", required = true) String hostName,
			                          @RequestParam(value = "metricName", required = true) String metricName,
			                          @RequestParam(value = "mediaName", required = true) String mediaName,
			                          @RequestParam(value = "triggerFuncName", required = true) String triggerFuncName,
			                          @RequestParam(value = "funcParameter", required = true) Integer funcParameter,
			                          @RequestParam(value = "paraUnit", required = true) String paraUnit,
			                          @RequestParam(value = "operator", required = true) String operator,
			                          @RequestParam(value = "triggerThreshold", required = true) double triggerThreshold,
			                          @RequestParam(value = "recoverThreshold", required = true) double recoverThreshold,
			                          @RequestParam(value = "faultTimes", required = true) Byte faultTimes,
			                          @RequestParam(value = "description", required = true) String description,
			                          @RequestParam(value = "isEnabled", required = true) Byte isEnabled,
			                          @RequestParam(value = "severe", required = true) Byte severe) {
		Long hostId = ruleService.selectHostId(hostName);
		Long metricId = ruleService.selectMetricId(metricName);
		Long userId = ruleService.selectUserId("admin");
		Byte mediaId = ruleService.selectMediaName(mediaName);
		Long triggerFuncId = ruleService.selectTriggerFuncId(triggerFuncName);
		Byte isAlerted = 1;
		Rule rule = new Rule(hostId,metricId,userId, mediaId,triggerFuncId,ruleName, funcParameter,paraUnit,
				operator, new BigDecimal(triggerThreshold),new BigDecimal(recoverThreshold), faultTimes,
				description,isEnabled, isAlerted, severe, new Date(), new Date());
		System.out.println(ReflectionToStringBuilder.toString(rule));
		int state =ruleService.insertRule(rule);
		System.out.println(state);
		return state;
	}
	 
	 @RequestMapping(value="/show", method={RequestMethod.GET}, produces="application/json;charset=UTF-8")
	 public @ResponseBody List<Rule> ruleList(ModelAndView modelAndView) throws Exception{
		 List<Rule> list = ruleService.findRuleDetail();
		 return list;
	 }
	 
	 @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	 public void deleteRule(@RequestParam(value = "id", required = true) Long id){
		 ruleService.deleteRule(id);
	 }
}
